<?php
	class houseControl extends Control{
		public $db;
		public function __auto(){
			indexControl::get_citys_view();
			$this->db = M();
			$this->auto_update_orderdata();
		}
		public function auto_update_orderdata(){
			$db = M("order");
			$rows = $db->all();
			$rows = $rows?$rows:array();
			foreach($rows as $k=>$v){
				if(time()>($v['xiadantime']+60*45) && $v['ispay'] == 0){ //如果超过45分钟就会将状态变成取消
					//$sql = "UPDATE tj_order SET `oid`='".$v['oid']."',`status` = 0 WHERE oid = '65530'";
					//$db->where(" oid = '".$v['oid']."'")->exe($sql);
					$db->update(array("status" => 0,'oid'=>$v['oid']));
				}
				//如果超过3天，就直接删除
				if(time()>($v['xiadantime']+60*60*24*3) && $v['ispay'] == 0){
					$db->where(" oid = '".$v['oid']."'")->del();
				}
			}
		}
		public function index(){
			$this->house();
		}
		//显示房屋
		public function house(){
			$hid = isset($_GET['hid']) && !empty($_GET['hid'])?$_GET['hid']:false;
			if($hid === false){
				$this->error('访问参数不正确',__WEB__);
			}
			$this->get_house_info($hid); //获得房屋的信息
			$this->get_sheshi_view();
			$this->get_house_sheshi($hid); //分配当前房屋设施信息
			$this->get_house_position($hid); //获得当前房屋的位置(房屋家居等)信息
			$this->get_house_otherserver($hid);//获得当前房屋的其他服务项目
			$this->get_house_fuwu($hid);//获得当前房屋的服务项目
			$this->get_house_tese($hid);
			$this->get_house_youhui($hid);
			$this->get_xqsheshi_view(); //获得所有的小区信息
			$this->display();
		}
		private function get_house_info($hid){
			$db = V('house');
			$db->view = array(
					'citys'=>array(
							'type'=>'inner',
							'field'=>'cname,cid',
							'on'=>'house.cid = citys.cid',
						),
					'house_data'=>array(
							'type'=>'inner',
							'on'=>'house.id = house_data.hid',
						),
					'xiaoqu'=>array(
							'type'=>'inner',
							'on'=>'house.qid = xiaoqu.qid',
						),
					'companys'=>array(
							'type'=>'inner',
							'field'=>'comname,comid',
							'on'=>'house.company = companys.comid',
						),
					"house_jingguan"=>array(
							'type'=>'inner',
							'on'=>'house.jingguan = house_jingguan.jgid',
						),
				);
			$house = $db->where("house.status = 1")->where(" id = '".$hid."'")->find();
			$house = $this->do_geju($house);
			$house['xqsheshi'] = $this->do_select($house['sheshi'], ',');
			$house = $this->get_images($house);//处理房屋的图片
			$this->get_rand_house($house['cid'],$house['id']);// 随机取得房屋的 附近房屋
			$this->assign('house',$house);
		}
		//随机取得附近的房屋4套
		private function get_rand_house($cid,$hid){
			$sql = "select title,geju,num,id,cid,thumb from tj_house where cid = ".$cid." and rand() limit 4";
			$db = M("house");
			$rows = $db->query($sql);
			$result = array();
			foreach($rows as $k=>$v){
				if($v['id'] != $hid){
					$result[] = $rows[$k];
				}
			}
			$result = $this->do_geju($result,2);
			$this->assign('fujin_house',$result);
		}
		//处理img图片
		public function get_images($house){
			if(empty($house['images'])){
				$house['bigimg'] = array();
				$house["minimg"] = array();
			}else{
				eval('$house["bigimg"]='.$house['images'].';');
				eval('$house["minimg"]='.$house['images_m'].';');
			}
			return $house;
		}
		//处理房屋格局
		public function do_geju($house,$type=1){
			$house = !empty($house)?$house:array();
			if($type == 1){ //一维数组时
				$geju = explode(",",$house['geju']);
				$house['geju'] = $geju[0]."室".$geju[1].'厅'.$geju[2].'卫';
				return $house;
			}elseif($type == 2){ //二维数组时
				$temp = array();
				foreach($house as $k=>$v){
					$temp = explode(',',$v['geju']);
					$house[$k]['geju'] = $temp[0]."室".$temp[1].'厅'.$temp[2].'卫';
				}
				return $house;
			}
			
		}
		private function get_sheshi_view(){
			$db = M("house_sheshi");
			$sheshi = $db->all();
			$this->assign("sheshi",$sheshi);
		}
		//获得当前房屋的服务项目
		private function get_house_fuwu($hid){
			$fuwu = $this->db->table("house_fuwu_rel")->where(" hid = '".$hid."'")->all();
			$fuwu = $fuwu?$fuwu:array();
			$fuwu = $this->getArrFieldByKey($fuwu, 'fwid');
			$fuwu_str = implode(',',$fuwu);
			$where_fw = $fuwu?" fwid in (".$fuwu_str.")":false;
			if($where_fw === false){
				$self_fuwu = array();
			}else{
				$self_fuwu = $this->db->table('house_fuwu')->where($where_fw)->all();
			}
			$this->assign("self_fuwu",$self_fuwu);
		}
		//获得当前房屋的样式
		private function get_house_tese($hid){
			$tese = $this->db->table('house_tese_rel')->where(" hid = '".$hid."'")->all();
			$tese = $tese?$tese:array();
			$tese = $this->getArrFieldByKey($tese, 'tsid');
			$tese_str = implode(',',$tese);
			$where_tese = $tese?" tsid in(".$tese_str.")":false;
			if($where_tese === false){
				$tese_self = array();
			}else{
				$tese_self = $this->db->table("house_tese")->where($where_tese)->all();
			}
			$this->assign('house_tese',$tese_self);
		}
		//获得房屋设施ID
		private function get_house_sheshi($hid){
			$db = M("house_sheshi_rel");
			$sheshi = $db->where(" hid = '".$hid."'")->all();
			$sheshi = $sheshi?$sheshi:array();
			$a = $this->getArrFieldByKey($sheshi, 'hsid');
			$this->assign('house_sheshi', $a);
			return $a;
		}
		//获得房屋的优惠信息
		private function get_house_youhui($hid){
			$youhui = $this->db->table("house_youhui")->where(" hid = '".$hid."'")->all();
			if(!$youhui){
				$youhui = array();
			}
			$this->assign("youhui",$youhui);
		}
		//获得其他服务
		private function get_house_otherserver($hid){
			$db = M("house_otherserver");
			$otherserver = $db->where(" hid = '".$hid."'")->all();
			$this->assign("otherserver",$otherserver);
		}
		//获得当前房屋的position值
		private function get_house_position($hid){
			$db = M("house_position");
			$position = $db->where(" hid = '".$hid."'")->all();
			$this->assign("position",$position);
		}
		private function get_xqsheshi_view(){
			$xqsheshi = $this->db->table("xqsheshi")->all();
			$this->assign("xqsheshi",$xqsheshi);
			
		}
		public function do_select($form,$str){
			$result = explode($str,$form);
			return $result;
		}
		//接受用户订单
		public  function do_order(){
			$hid = $_POST['hid'];
			$cid = $_POST['cid'];
			$starttime = strtotime($_POST['startDate']);
			$endtime = strtotime($_POST['endDate']);
			$db = M("order");
			$row = $db->where(" hid = '".$hid."'")->find();
			if($row){
				if($row['status'] == 1){
					if($row['endtime'] < $starttime){
						$this->error('该房屋已经被预定走了！',__WEB__.'/index/index/search/cid/'.$cid);
					}else{
						$this->error('该时段已经被预订了！',__WEB__.'/index/index/search/cid/'.$cid);
					}
				}
				/**$row = $row?$row:array();
				if($row['endtime'] < $starttime && $row['status'] == 0){
					if( $row && $row['status'] == 1 && ((time()- $row['xiadantime'])<(60*1) || $row['ispay'] == 1)){
						$this->error('该房屋已经被预定走了！',__WEB__.'/index/index/search/cid/'.$cid);
					}
				}else{
					$this->error('该时段已经被预订了！',__WEB__.'/index/index/search/cid/'.$cid);
				}**/
			}
			
			$this->get_order_house_info($hid);
			$this->display();
		}
		public function get_order_house_info($hid){
			$db = V("house");
			$db->view = array(
					'citys'=>array(
							'type'=>'inner',
							'field'=>'cname,cid',
							'on'=>'house.cid = citys.cid',
					),
					'companys'=>array(
							'type'=>'inner',
							'field'=>'comname,comid',
							'on'=>'house.company = companys.comid',
					),
			);
			$house = $db->where(" id = '".$hid."'")->find();
			$house = $this->do_geju($house);
			//P($house);
			$fei = array();
			$fei['startdate'] = isset($_POST['startDate'])?$_POST['startDate']:$_POST['startdate'];
			$fei['enddate'] = isset($_POST['endDate'])?$_POST['endDate']:$_POST['enddate'];
			$fei['daynum'] = (strtotime($fei['enddate'])-strtotime($fei['startdate']))/(60*60*24);
			$fei['money'] = $fei['daynum']*$house['price'];
			$_SESSION['house'] = $house; //将要预定的房屋的信息存入session
			$_SESSION['fei'] = $fei;  //将预定房屋费用信息存入到session中
			$this->assign("fei",$fei);
			$this->assign("house",$house);
		}
		// 提交订单数据
		public function tijiao_order(){
			if(!isset($_POST) ||!isset($_SESSION['fei']) || !isset($_SESSION['house'])){
				$this->error('访问参数不对',__WEB__);
			}
			$hid = $_POST['hid'];
			$_POST['xiadan'] = time(); //下蛋时间
			$house = $_SESSION['house']; //房屋信息
			$fei = $_SESSION['fei']; //费用信息
			$_SESSION['order'] = $order = $_POST;
			$this->get_order_house_info($hid);
			if(!isset($_SESSION['user']) || empty($_SESSION['user'])){
				go(__WEB__.'/index/login_reg/login');
			}else{
				$user = $_SESSION['user'];
				$data = array();
				$db= M("order");
				$data['uid'] = $user['uid'];
				$data['hid'] = $house['id'];
				$data['xiadantime'] = $order['xiadan'];
				$data['starttime'] = strtotime($order['startdate']);
				$data['endtime'] = strtotime($order['enddate']);
				$data['price'] = $fei['money'];
				$data['daynum'] = $fei['daynum'];
				$data['lianxiname'] = $order['v_contactname'];
				$data['lianximobile'] = $order['v_contactmobile'];
				$data['ruzhuname'] = $order['v_checkinname'];
				$data['ruzhumobile'] = $order['v_checkinmobile'];
				$data['lianxiemail'] = $order['v_contactemail'];
				$data['lianxitel'] = $order['v_contacttel'];
				$data['contract'] = $order['v_contract'];
				$data['liuyan'] = $order['liuyan'];
				$data['ispay'] = 0;
				$data['house_thumb'] = $house['thumb']; //房屋缩略图
				$data['house_title'] = $house['title']; //房屋标题
				$data['house_address'] = $house['address'];
				$data['house_geju'] = $house['geju'];
				$db->add($data);
				$this->session_del('house');
				$this->session_del("order");
				$this->session_del("fei");
			}
			$this->display();
		}
		//处理订单(在确认订单页面)
		public function ajaxdata(){
			C("debug",0);
			$result = ArrEncode($_POST);
			echo urldecode(json_encode($result));
		}
		//ajax计算房屋的价格
		public function ajax_order_price(){
			C("debug",0);
			$startdate = strtotime($_POST['startDate']);
			$enddate = strtotime($_POST['endDate']);
			$price = $_POST['price'];
			if(!empty($startdate) && !empty($enddate)){
				$length = ($enddate - $startdate)/(60*60*24);
				$str = '<dt>共入住 <span class="yanse">'.$length.'</span> 晚</dt>  
								<dd>
									<span class="l">原价</span>
									<span class="r"><del class="yanse"><em class="yen yanse">¥</em>'.$length*$price.'</del></span>
								</dd> 
								<dd>
									<span class="l">节省</span>
									<span class="r"><b class="yanse">0</b>（享受特价）</span>
								</dd>   
								<dd>
									<span class="l">合计</span>
									<span class="r"><em class="yen">¥</em><b class="yanse">'.$length*$price.'</b></span>
								</dd>   
								<dd>
									<span class="l">订金</span>
									<span class="r"><em class="yen">¥</em><b class="yanse">'.$length*$price.'</b>（订金为全额房费）</span>
								</dd>';
				echo $str;
			}
		}
		
		/**
		 * 根据键值来对二维数组组合 组成新的数组 只含有该键的值
		 * @param Array $arr
		 * @param String $field
		 * @return Array 重新组合好的数组
		 */
		public function getArrFieldByKey($arr,$field){
			$result = array();
			foreach($arr as $k=>$v){
				foreach($v as $m=>$n){
					if($m == $field){
						$result[] = $n;
					}
				}
			}
			return $result;
		}
	}

?>